﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Cashier_Interface;
using Cashier_Service;
using Cashier.utils;
using Cashier_Utils;

namespace Cashier.member
{
    public partial class MemberIntegralListControl : UserControl, IDataGridViewData
    {
        private MainForm mainform;
        private static MemberIntegralListControl memberIntegralListControl;
        private MemberIntegralService memberIntegralService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mainform"></param>
        private MemberIntegralListControl(MainForm mainform)
        {
            InitializeComponent();
            this.dataGridView1.ImplementsInstance = this;
            this.mainform = mainform;
            this.memberIntegralService = new MemberIntegralService();

            this.Dock = DockStyle.Fill;
        }

        /// <summary>
        /// 返回MemberIntegralListControl实例
        /// </summary>
        /// <param name="mainform"></param>
        /// <returns></returns>
        public static MemberIntegralListControl GetMemberIntegralListControl(MainForm mainform)
        {
            if (memberIntegralListControl == null)
                memberIntegralListControl = new MemberIntegralListControl(mainform);
            memberIntegralListControl.RefreshDataGridViewData();
            return memberIntegralListControl;
        }

        #region 功能方法实现

        /// <summary>
        /// 删除用户积分记录
        /// </summary>
        private void DeleteMemberIntegral()
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                DialogResult result = MessageBox.Show("确定要删除选中数据吗？", "提示", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {
                    StringBuilder sb = new StringBuilder();
                    foreach (DataGridViewRow row in dataGridView1.SelectedRows)
                    {
                        sb.Append(row.Cells[0].Value.ToString()).Append(",");
                    }
                    sb.Remove(sb.Length - 1, 1);
                    int count = memberIntegralService.DeleteMemberIntegral(sb.ToString());
                    if (count > 0)
                    {
                        MessageBox.Show("删除成功！");
                        this.RefreshDataGridViewData();
                    }
                    else
                        MessageBox.Show("删除失败！");
                }
            }
        }

        #endregion

        #region IDataGridViewData 成员

        void IDataGridViewData.EnterkeyEventHandler()
        {
            return;
        }

        void IDataGridViewData.GenerateGridViewColumns(DataGridView dgv)
        {
            this.dataGridView1.AutoGenerateColumns = false;
            this.dataGridView1.Columns.Clear();
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("id", null, true, 0, null, null, false, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("member_no", "会员编号", true, 20, ActKey.LEFT, ActKey.LEFT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("type", "类型", true, 10, ActKey.CENTER, ActKey.CENTER, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("integral", "积分", true, 10, ActKey.RIGHT, ActKey.RIGHT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("trans_no", "交易流水号", true, 20, ActKey.LEFT, ActKey.LEFT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("is_success", "状态", true, 10, ActKey.CENTER, ActKey.CENTER, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("description", "描述", true, 15, ActKey.LEFT, ActKey.LEFT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("trans_time", "发生日期", true, 15, ActKey.CENTER, ActKey.CENTER, true, false));
        }

        void IDataGridViewData.GetBindingData(int rowindex, int pageindex, int pagesize)
        {
            try
            {
                String enterpriseNo = mainform.Admin.EnterpriseNo;
                String adminUsername = mainform.UsernameForQuery;
                String memberno = textbox_memberno.Text.Trim();
                String transno = textbox_transno.Text.Trim();
                DataTable dt = memberIntegralService.GetMemberIntegralTable(pageindex, pagesize, enterpriseNo, memberno, 0, transno, 
                    null, adminUsername, null, "id");
                this.dataGridView1.DataSource = dt;
                if (dt != null && dt.Rows.Count > 0)
                {
                    if (rowindex > 0)
                        dataGridView1.Rows[0].Selected = false;
                    dataGridView1.Rows[rowindex].Selected = true;
                }
                dataGridView1.Columns[0].Visible = false;

                mainform.SetPageInfo(dataGridView1.Recordcount, pageindex, pagesize);
            }
            catch (Exception ex)
            {
                LogsUtil.Record(ex);
            }
        }

        long IDataGridViewData.GetBindingDataRecord()
        {
            long record = 0;
            try
            {
                String enterpriseNo = mainform.Admin.EnterpriseNo;
                String adminUsername = mainform.UsernameForQuery;
                String memberno = textbox_memberno.Text.Trim();
                String transno = textbox_transno.Text.Trim();
                record = memberIntegralService.GetMemberIntegralRecord(enterpriseNo, memberno, 0, transno, null, adminUsername);
            }
            catch (Exception ex)
            {
                LogsUtil.Record(ex);
            }
            return record;
        }

        #endregion

        #region 刷新数据

        /// <summary>
        /// 刷新数据
        /// </summary>
        public void RefreshDataGridViewData()
        {
            this.dataGridView1.LoadingData();
        }

        #endregion

        #region 重写事件

        /// <summary>
        /// 重写键盘事件
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="keyData"></param>
        /// <returns></returns>
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            bool b = base.ProcessCmdKey(ref msg, keyData);
            switch (keyData)
            {
                case Keys.Up:
                    this.dataGridView1.Focus();
                    break;
                case Keys.Down:
                    this.dataGridView1.Focus();
                    break;
                case Keys.F8:
                    this.RefreshDataGridViewData();
                    break;
            }
            return b;
        }

        #endregion

        #region 控件事件

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_query_Click(object sender, EventArgs e)
        {
            this.RefreshDataGridViewData();
        }

        /// <summary>
        /// 单元格格式化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.Value != null)
            {
                if (dataGridView1.Columns[e.ColumnIndex].Name.Equals("type"))
                {
                    if (Int32.Parse(e.Value.ToString()) == (int)IntegralEnum.IN)
                        e.Value = "增长";
                    else if (Int32.Parse(e.Value.ToString()) == (int)IntegralEnum.OUT)
                        e.Value = "消费";
                }
            }
        }

        /// <summary>
        /// 删除会员积分记录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripMenuItem_delete_Click(object sender, EventArgs e)
        {
            this.DeleteMemberIntegral();
        }

        #endregion
    }
}
